package com.xidian.zhiancloud.service.emergencyManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.EmergencyMaterialsInventoryManagementMapper;
import com.xidian.zhiancloud.dao.ReviewRecordMapper;
import com.xidian.zhiancloud.dao.WarehousingMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.EmergencyMaterialsInventoryManagement;
import com.xidian.zhiancloud.model.ReviewRecord;
import com.xidian.zhiancloud.model.Warehousing;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.vo.emergencyManagement.ReviewRecordVO;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehousingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WarehousingService extends ServiceImpl<WarehousingMapper, Warehousing> implements IService<Warehousing> {

    @Autowired
    private WarehousingMapper warehousingMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EmergencyMaterialsInventoryManagementMapper emergencyMaterialsInventoryManagementMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean add(Warehousing warehousing) {
        int result = warehousingMapper.insert(warehousing);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        Long numIn=warehousing.getQuantity();
        Long emergencyMaterialsId=warehousing.getEmergencyMaterialsId();
        EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement=emergencyMaterialsInventoryManagementMapper.selectById(emergencyMaterialsId);
        Long numE=emergencyMaterialsInventoryManagement.getNum();
        emergencyMaterialsInventoryManagement.setNum(numIn+numE);
        int i = emergencyMaterialsInventoryManagementMapper.updateById(emergencyMaterialsInventoryManagement);
        return i==1;
    }

    public List<WarehousingVO> getWarehousing(Long id) {
        LambdaQueryWrapper<Warehousing> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(Warehousing::getEmergencyMaterialsId,id);
        List<Warehousing> list=warehousingMapper.selectList(queryWrapper);
        List<WarehousingVO> ans=new LinkedList<>();
        list.forEach(t -> {
            WarehousingVO vo = new WarehousingVO();
            BeanUtils.copyProperties(t, vo);
            SysUser user=sysUserMapper.selectById(vo.getAgentId());
            vo.setAgentName(user==null?"该用户已删除":user.getName());

            ans.add(vo);
        });
        return ans;
//        for (Warehousing an : ans) {
//            WarehousingVO tmp=new WarehousingVO();
//            BeanUtils.copyProperties(an,tmp);
//            Long agentId=an.getAgentId();
//            SysUser user= sysUserMapper.selectById(agentId);
//            tmp.setAgentName(user.getUsername());
//            warehousingVOList.add(tmp);
//        }
//        return warehousingVOList;
    }

}
